var d1 = require("deviceone");
var dataCache = d1.sm("do_DataCache");
var do_App = d1.sm("do_App");
var do_Global = d1.sm("do_Global");
var do_Network = d1.sm("do_Network");
var dojs = require("dojs");

/**
 * 检查本地是否有用户信息
 */
module.exports.hasUserInfo = function(userDataKey) {

	// 加载用户信息
	var userData = null;
	//dataCache.removeData(userDataKey);
	var isHas = dataCache.hasData(userDataKey);
	// 根据IP保存位置信息
	saveLocation();
	// 如果有则直接从缓存读取跳转首页，否则跳转登陆界面
	if (isHas) {
		userData = dataCache.loadData(userDataKey);
		// 打开首页页面
		var entrance = require("source://script/mymodule/entrance");
		entrance.openHome.openHomePage();
	} else {
		// 打开登录页面
		var entrance = require("source://script/mymodule/entrance");
		entrance.openLogin.openLoginPage();
	}
	do_App.closePageToID("welcomePage");
};

// 保存位置信息
function saveLocation() {
	var province = null;
	var city = null;
	var address = null;
	var dataLabel = do_Global.getMemory("data_label");
	// 判断是否有网络
	if (dojs.core.valueEqual(do_Network.getStatus(), "none")) {
		address = " ";
		do_Global.setMemory(dataLabel.location, address);
		return;
	}
	var url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json";
	dojs.http.ajax({
		url : url,
		type : "GET",
		success : function(data) {
			// 保存
			var province = data.province+"省";
			var city = data.city+"市";
			var address = province == city?province:province+" "+city;
			do_Global.setMemory(dataLabel.location, address);
		}
	});
}
